import type { Context } from 'koa';
import { UserService } from '@/services/user';
import { DelPhotoDto } from '@/dtos/delPhoto.dto';
import { PhotoService } from '@/services/photo';
import { UserRoles } from '@/enums/userEnums';
import { ResponseErrors } from '@/enums/responseErrors';

export const photoDelController = async (ctx: Context) => {
  const input = ctx.request.body;
  const params = await ctx.vailParams<DelPhotoDto>(DelPhotoDto, input);

  const photoId = params.photoId;
  const userId = params.userId || ctx.userId;
  /**
   * 如果不是管理员，无权删除他人的照片
  */
  if (ctx.user.role !== UserRoles.admin) {
    if (userId !== ctx.userId) {
      ctx.sendError(ResponseErrors.userNoPermissionDelPhoto);
      return;
    }
  }
  await Promise.all([
    PhotoService.delPhoto(photoId),
    UserService.delUserPhoto({ userId, photoId }),
  ]);
  ctx.sendSuccess();
}